import { imgUrl, freeBarrierIconPaths, markerTypes } from '../config/config'

const maxLen = 50;

const valConfig = {
  name: {
    required: '需要填写姓名',
    reg: /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/,
    regErr: '请填写正确的姓名',
  },
  phone: {
    required: '需要填写手机号',
    reg: /^1[0-9]{10}$/,
    regErr: '请填写正确的手机号'
  },
  idcard: {
    required: '需要填写证件号',
    reg: /(^\d{22}$)|(^\d{20}$)/,
    regErr: '请填写正确的格式'
  },
  departure: {
    required: '需要选择出发地',
  },
  dest: {
    required: '需要填写目的地',
  }
}

function validateData(data) {

  var item;
  var errResult = {};

  for (item in valConfig) {

    errResult[item] = {};

    if (data[item] == undefined || !data[item].trim()) {

      errResult[item]['regErrMsg'] = valConfig[item]['required'];

    } else if (data[item].length > maxLen) {

      errResult[item]['regErrMsg'] = '填写内容过长';

    } else if (valConfig[item]['reg']) {

      if (!valConfig[item]['reg'].test(data[item])) {

        errResult[item]['regErrMsg'] = valConfig[item]['regErr'];

      }

    }

    //假如没有错误，则错误信息为空字符串
    if (JSON.stringify(errResult[item]) == "{}") {
      errResult[item] = '';
    }

  }

  return errResult;

}

/**
 * 出行Order的data Array转换为markers 
 * @param {Array} pointList 
 * @param {String} key list里面对应哪个key的value对应的point对象，并且具有lat和lng属性
 * @param {Object} markerType 对应使用哪种marker类型，在config文件里面有配置
 */
function transformPointsToMarkers(pointList, key, markerType) {
  return pointList.map(item => {
    var data = {
      id: item.id,
      latitude: item[key].lat,
      longitude: item[key].lng
    }
    return Object.assign({}, markerType, data)
  })
}

/**
 * 转换无障碍坐标点未marker数组
 * 
 * @param {Array} pointList 
 * @param {Array} coordCat 建筑分类 
 * @param {Number} activeId  当前已经选择中的markerid
 */
function transformFreeBarrierPointsToMarkers(pointList, coordCat, activeMarkerId, markerTypeObj,markerTypeActObj) {

  var data,test;

  return pointList.map(item => {

    data = {
      id: Number(item.id),
      iconPath: coordCat ? getIconPathByTypeId(item.category, coordCat) : markerTypeObj.iconPath,
      latitude: item.lat,
      longitude: item.lng
    }

    if (item.id != activeMarkerId) {
      return Object.assign({}, markerTypeObj, data)
    } else {
      data.iconPath = data.iconPath.replace('.png', '_act.png')
      
      test = markerTypeActObj ? Object.assign({},  data,markerTypeActObj) : Object.assign({}, markerTypeObj, data) 
      
      return test
    }

  })

}

/**
 * 通过id获取地点的类型的icon路径
 * 
 * @param {Number} id 
 * @param {Array} coordCat 
 */
function getIconPathByTypeId(category, coordCat) {

  var iconPath = '';
  coordCat.map(item => {
    if (item.catid == category) {
      // iconPath = item.icon_url
      iconPath = getIconPath(item.internal_name);
    }
  })
  return iconPath
}

function getIconPath(name) {

  var iconPath = '';
  var itemName;
  var item;

  for (itemName in freeBarrierIconPaths) {
    item = freeBarrierIconPaths[itemName];

    if (item.internal_name == name) {
      iconPath = item.path;

    }
  }

  return iconPath;
}

/**
 * 获取当前激活的地点的数据详情
 * @param {Array} coordResults 当前的地点的列表
 * @param {Number} id 
 */
function getCoorDataById(coordResults, id) {
  

  var result;

  coordResults.map(item => {
    if (item.id == id) {
      result = item;
    }
  })

  return result;

}

/**
 * 把result 的facilities 字符串转成 markerData需要的数组
 * 
 * @param {Object} result 
 * @param {Array} barrierType 
 * @returns 
 */
function transformFacStrToArray(result, barrierType) {
  
  var facArr = [];
  var facResult = [];
  if (result.facilities) {
    facArr = result.facilities.split(',');

    barrierType.map(item => {

      if (facArr.indexOf(item.facid) > -1) {

        facResult.push({
          facid: item.facid,
          icon_url: imgUrl + item.icon_url,
          name: item.name
        })

      }

    })

  }

  return Object.assign({}, result, {
    facilities: facResult
  })

}

function activeMarker(markerList, id,markerTypeActObj,markerTypeObj) {

  var newMarkerList,activeItem;

  newMarkerList = markerList.map(item => {
    item = Object.assign({}, item,markerTypeObj)
    item.iconPath = item.iconPath.replace('_act', '');
    if (item.id == id) {
      activeItem = item;
      item = Object.assign({}, item, markerTypeActObj);
      item.iconPath = item.iconPath.replace(".png", "_act.png");
    }
    return item;
  })

  return {
    newMarkerList:newMarkerList,
    activeItem:activeItem
  }
  

}

module.exports = {
  // formatTime: formatTime,
  validateData: validateData,
  transformPointsToMarkers: transformPointsToMarkers,
  transformFreeBarrierPointsToMarkers: transformFreeBarrierPointsToMarkers,
  getCoorDataById: getCoorDataById,
  transformFacStrToArray: transformFacStrToArray,
  activeMarker:activeMarker
// homeControllers:homeControllers
}
